#include "usartconf.h"
//#include "uart0.h"


void uart_send_data(uint32_t usart_periph, uint8_t *data, uint16_t length)
{
	int i = 0;
	for(i = 0; i < length; i++ )
	{
		usart_flag_clear(usart_periph,USART_FLAG_TC);
		usart_data_transmit(usart_periph, data[i]);
		while(RESET == usart_flag_get(usart_periph, USART_FLAG_TC));
	}
}

void usart_config()
{
	/* USART5 interrupt configuration */
	nvic_irq_enable(USART5_IRQn, 0, 0);
	rcu_periph_clock_enable(RCU_GPIOC);    
	rcu_periph_clock_enable(RCU_USART5);
	/* connect port to USART5_Tx */
	gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_6);
	/* connect port to USART5_Rx */
	gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_7);
	/* configure USART5 Tx as alternate function push-pull */
	gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
	gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
	/* configure USART5 Rx as alternate function push-pull */
	gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
	gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
	/* USART5 configure */
	usart_deinit(USART5);
	usart_baudrate_set(USART5,115200U);
	usart_parity_config(USART5,USART_PM_NONE);
	usart_word_length_set(USART5,USART_WL_8BIT);
	usart_stop_bit_set(USART5,USART_STB_1BIT);
	//usart_receive_config(USART5, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART5, USART_TRANSMIT_ENABLE);
	/* enable USART5 receive interrupt */
	//usart_interrupt_enable(USART5, USART_INTEN_RBNEIE);
	//usart_interrupt_enable(USART5, USART_INTEN_IDLEIE);
//	usart_interrupt_disable(USART5,USART_INTEN_TCIE);
//	usart_interrupt_disable(USART5,USART_INTEN_TBEIE);
	
	/* enable USART5 */
	usart_enable(USART5);
}
